<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'', styles:'bootstrap,font-awesome,animate,style,bootstrap-table,layui-date,auto-complete,dtvalidate'}){%>
    <style type="text/css">
        body{padding-top:10px;}
        *{margin:0;padding:0;list-style-type:none;}
        a,img{border:0;}
        .demo h2{font-size:16px;color:#3366cc;height:30px;}
        .demo li{float:left;}
    </style>
    <%}%>
</head>

<body>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins" style="margin-bottom: 0px;">
                <div class="ibox-content" >
                    <form id="search-form" role="form" class="form-horizontal" autocomplete="off">
                        <div class="form-group">
                            <label class="control-label col-sm-1">柜号:</label>
                            <div class="col-sm-2">
                                <input type="text" id="createPlanNo" name="createPlanNo" placeholder="请输入柜号" class="form-control">
                            </div>
                            <label class="control-label col-sm-1">SKU:</label>
                            <div class="col-sm-2">
                                <input type="text" id="createSku" name="createSku" placeholder="请输入SKU" class="form-control">
                            </div>
                            <label class="control-label col-sm-1">PO:</label>
                            <div class="col-sm-2">
                                <input type="text" id="erpProducePo" name="erpProducePo" placeholder="请输入PO" class="form-control">
                            </div>
                            <a class="search-btn btn btn-success" style="margin-bottom: 0px;">查询</a>
                        </div>
                    </form>
                </div>
            </div>
            <!-- START 右侧数据 -->

            <!-- START 操作按钮 -->
            <!-- END 操作按钮 -->
            <div id="toolbar" class="btn-group" style="margin-bottom:5px;">
                <a class="export-btn btn btn-sm btn-success" style="margin-left: 5px"><i class="fa fa-plus" aria-hidden="true"></i> 导出当前数据</a>
            </div>
            <!-- START 数据表格 -->
            <table id="planTable"
                   data-toolbar="#toolbar"
                   data-pagination="true"
                   data-resizable="true"
                   data-side-pagination="server"
                   data-url="/sea/physical/distribute/findByCnd.do"
                   data-page-size="15"
                   class="table table-striped table-hover"
            >
            </table>
            <!-- END 数据表格 -->

            <!-- END 右侧数据 -->
        </div>
    </div>
</div>

<!-- START 内容部分 -->
<div class="container" style="display: none;overflow-x: hidden;max-width: 600px" id="layer_window">
    <form class="form-horizontal" id="info-form" enctype="multipart/form-data" autocomplete="off" >
        <input type="hidden" name="createId" id="id">
        <br/>
        <div class="form-group">
            <label class="col-sm-2"></label>
            <div class="col-sm-3">
                <a href="/excel/自动分配报表.xls" download>点击下载模板</a>
            </div>
        </div>

        <div class="form-group" style="max-width: 600px;min-width: 580px">
            <label class="control-label col-sm-2">选择文件:</label>
            <div class="col-sm-6">
                <a class="club-import-btn">
                    <input type="file" id="importSession" name="importSession">
                </a>
            </div>
        </div>
    </form>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="save-btn btn btn-primary">
                <i class="fa fa-save"></i> 保存
            </button>
        </div>
    </div>
</div>
<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,dtvalidate,dtvalidate-lang-zh_cn,bootstrap-table-zh-CN,laydate,layer'}){}%>
<script>
    var $planTable = $('#planTable');
    $('.save-btn').on('click',function(){
        layer.closeAll();
        layer.load();
        importExcel();
    });

    function importExcel() {
        $.ajax({
            url: "/sea/physical/distribute/auto/importAutoDistribute",
            data:  new FormData($('#info-form')[0]),
            type: "post",
            // async: false,
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            beforeSend:function () {
                layer.closeAll();
                layer.load(1, {
                    shade: [0.1,'#fff'], //0.1透明度的白色背景
                    zIndex : 1
                });
            },
            success: function (data) {
                if(data.ok){
                    layer.closeAll();
                    layer.msg("上传成功！",{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    $planTable.bootstrapTable('refresh');
                }else{
                    layer.closeAll(); //关闭弹窗
                    layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){
                            layer.closeAll();
                        }});
                }
            },
            error:function (data) {
                layer.msg("接口异常!请联系管理员!",{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){
                        layer.closeAll();
                    }});
            }
        });
    }

    $(function () {

        $('.export-btn').on('click',function () {
            var selDatas = $planTable.bootstrapTable('getSelections');
            console.log(selDatas);
            if( selDatas.length == 0 ){
                layer.msg('请选择要导出的',{icon: 0,shade: [0.8, '#393D49'],time: 1500});
            }else{
                var ids = [],names = [];
                $.each(selDatas,function(i,d){
                    if(d.createPlanStatus !== 2)
                    {
                        layer.msg('无法导出未处理的记录!');
                        return false;
                    }
                    else
                    {
                        ids.push(d.id);
                    }
                });
                ids = ids.join(',');
                window.location.href='/sea/physical/distribute/export.do?ids='+ids;
            }
        });

        // START 表格配置
        $planTable.bootstrapTable({
            pageSize:10,
            detailView: true,
            columns: [
                {
                    field: 'state',
                    checkbox: true,
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '柜号',
                    field: 'createPlanNo',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '柜型',
                    field: 'createPlanCabinetName',
                    align: 'center',
                    valign: 'middle'
                }, {
                    title: '运输',
                    field: 'createPlanTransport',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '国家',
                    field: 'createPlanCountry',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '装柜地点',
                    field: 'createPlanPlace',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '装柜时间',
                    field: 'createPlanPlaceDate',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '总件数',
                    field: 'createPlanTotal',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '总箱数',
                    field: 'createPlanBox',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '总体积',
                    field: 'createPlanVolume',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '小件数',
                    field: 'createPlanNumber',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '总毛重',
                    field: 'createWeightTotal',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '总净重',
                    field: 'createKgTotal',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '状态',
                    field: 'createPlanStatus',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        var result='';
                        switch (v) {
                            case 1:
                                result='未处理';
                                break;
                            case 2:
                                result='已分配';
                                break;
                            case 22:
                                result='待分配';
                                break;
                            case 3:
                                result='已发运';
                                break;
                            case 4:
                                result='已开船';
                                break;
                            case 5:
                                result='已清关';
                                break;
                            case 6:
                                result='已送货';
                                break;
                            case 7:
                                result='已入库';
                                break;
                            default:
                                result='';
                        }
                        return result;
                    }
                },
                {
                    title: '操作',
                    field: 'operate',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter
                }
            ],
            onLoadSuccess : function(){
                $('.export-auto-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    //分配取消计划
                    layer.confirm('确定导出当前分配计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        exportAuto2(id);
                    });
                });
                $('.cancel-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    //分配取消计划
                    layer.confirm('确定取消分配计划1？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        cancel(id);
                    });
                });
                $('.sure-auto-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    //分配发运计划
                    layer.confirm('确定要自动占有当前预分配计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        sureAutoDistribute(id);
                    });
                });
                $('.import-auto-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    //分配发运计划
                    layer.confirm('确定要自动分配当前计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        autoDistribute(id);
                    });
                });
                $('.distribute-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    //分配发运计划
                    layer.confirm('一经分配,无法取消,确定要分配当前计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        distribute(id);
                    });
                });

                $('.cancel-auto-distribute-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    //分配取消计划
                    layer.confirm('确定取消分配计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        cancelAutoDistribute(id);
                    });
                });
                $('.send-btn').on('click',function () {
                    var id = $(this).attr('data-val');
                    //分配发运计划
                    layer.confirm('确定要下推当前计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        var index = layer.load();
                        $.post('/sea/physical/distribute/send.do',{id:id},function(data){
                            if( data.ok ){
                                layer.closeAll(); //关闭弹窗
                                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                                    $planTable.bootstrapTable('refresh');
                                });
                            }else{
                                layer.closeAll(); //关闭弹窗
                                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                                        layer.closeAll();
                                    }});
                            }
                        });
                    });
                });

                $('.send-fw-btn').on('click',function () {
                    var id = $(this).attr('data-val');
                    //分配发运计划
                    layer.confirm('确定要推送当前质检计划？', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        var index = layer.load();
                        $.post('/sea/physical/distribute/auto/sendFw.do',{id:id},function(data){
                            if( data.ok ){
                                layer.closeAll(); //关闭弹窗
                                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                                    $planTable.bootstrapTable('refresh');
                                });
                            }else{
                                layer.closeAll(); //关闭弹窗
                                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                                        layer.closeAll();
                                    }});
                            }
                        });
                    });
                });
                $('.detail-btn').on('click',function(){
                    var id = $(this).attr('data-val');
                    layer.open({
                        type: 2,
                        area: ['700px','500px'],
                        content: ['/sea/physical/record/main.do?createId='+id, 'yes']
                    });
                });

                $(".import-btn").on('click',function () {
                    var id = $(this).attr('data-val');
                    $("#id").val(id);
                    layer.open({
                        type:1,
                        title:'导入明细',
                        area:['600px','400px'],
                        skin:'layer-window',
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_window')
                    });
                });
            },onExpandRow: function (index, row, $detail) {
                InitSubTable(index, row, $detail);
            }
        });

        //初始化子表格(无线循环)
        function InitSubTable (index, row, $detail) {
            var parentId = row.id;
            var cur_table = $detail.html('<table id="disTable"></table>').find('table');
            $(cur_table).bootstrapTable({
                url: '/sea/physical/distribute/findByCreateId',
                method: 'get',
                clickToSelect: true,
                queryParams: { createId: parentId},
                ajaxOptions: { id: parentId },
                clickToSelect: true,
                //height: 500,
                detailView: false,//父子表
                uniqueId: "id",
                pageSize: 10,
                pageList: [10, 25],
                columns: [
                    {
                        field: 'createShipmentId',
                        title: 'ShipmentId'
                    },
                    {
                        field: 'createSku',
                        title: 'SKU'
                    },{
                        field: 'createFnSku',
                        title: 'FNSKU'
                    },{
                        field: 'productName',
                        title: '产品名称'
                    },{
                        field: 'productColorName',
                        title: '颜色'
                    },{
                        field: 'productCm',
                        title: '尺码(CM)'
                    },{
                        field: 'productInch',
                        title: '尺码(INCH)'
                    },{
                        field: 'createNumber',
                        title: '数量'
                    },{
                        field: 'createPackage',
                        title: '装箱量'
                    },{
                        field: 'createBoxNumber',
                        title: '箱数'
                    },{
                        field: 'productContainCount',
                        title: '内含件数'
                    },{
                        field: 'createNumberTotal',
                        title: '总件数'
                    },{
                        field: 'createSupplierName',
                        title: '供应商'
                    },{
                        field: 'createWeightTotal',
                        title: '总毛重'
                    },{
                        field: 'createKgTotal',
                        title: '总净重'
                    },{
                        field: 'createVolumeTotal',
                        title: '总体积'
                    },{
                        field: 'erpProducePo',
                        title: 'PO号'
                    },{
                        field: 'erpProducePrice',
                        title: '采购单价'
                    },
                    {
                        title: '单位',
                        field: 'productUnit',
                        align: 'center',
                        valign: 'middle'
                    },
                    {
                        title: '报关名称',
                        field: 'productDeclarationName',
                        align: 'center',
                        valign: 'middle'
                    },
                    {
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter2
                }],
                onLoadSuccess : function(){
                    $('.edit-sku-btn').on('click',function(){
                        var id = $(this).attr('data-val');
                        layer.open({
                            type: 2,
                            area: ['500px','400px'],
                            content: ['/sea/physical/distribute/editPage.do?id='+id, 'yes']
                        });
                    });
                }
            });
        };
    });

    // 操作菜单
    var status;
    function optFormatter(value,row){
        var content = '';
        status=row.createPlanStatus;
        if(row.createPlanStatus==1){
            content += '&nbsp;<button class="import-auto-btn btn btn-xs btn-success" data-val="'+row.id+'" title="自动分配">  <i class="fa fa-check-square-o"></i>自动分配</button>';
            content += '&nbsp;<button class="import-btn btn btn-xs btn-success" data-val="'+row.id+'" title="导入分配">  <i class="fa fa-check-square-o"></i>导入分配</button>';
        }
        if(row.createPlanStatus==2){
            content += '&nbsp;<button class="cancel-btn btn btn-xs btn-danger" data-val="'+row.createId+'" title="取消分配"><i class="fa fa-check-square-o"></i> 取消分配</button>';
            content += '&nbsp;<button class="export-auto-btn btn btn-xs btn-warning" data-val="'+row.createId+'" title="导出分配"><i class="fa fa-download"></i> 导出分配</button>';
        }
        if(row.createPlanStatus==22){
            content += '&nbsp;<button class="cancel-auto-distribute-btn btn btn-xs btn-danger" data-val="'+row.createId+'" title="取消分配"><i class="fa fa-check-square-o"></i> 取消分配</button>';
            content += '&nbsp;<button class="sure-auto-btn btn btn-xs btn-success" data-val="'+row.id+'" title="确认分配">  <i class="fa fa-check-square-o"></i>确认分配</button>';
        }
        if(row.count==0 && row.createPlanStatus!=1 && row.createPlanStatus!=22){
            content += '&nbsp;<button class="send-btn btn btn-xs btn-success" data-val="'+row.id+'" title="下推至发运"><i class="fa fa-check-square-o"></i> 下推至发运</button>';
            if(row.fileCount==0){
                content += '&nbsp;<button class="send-fw-btn btn btn-xs btn-success" data-val="'+row.id+'" title="下推至发运"><i class="fa fa-check-square-o"></i> 推送至泛微</button>';
            }
        }



        content += '&nbsp;<button class="detail-btn btn btn-xs btn-default" data-val="'+row.id+'" title="查看记录"> 查看记录</button>';
        return content;
    }
    // 操作菜单
    function optFormatter2(value,row){
        var content ='';
        if(row.createSupplierName=='尚佰' && status<3){
            content+= '<button class="edit-sku-btn btn btn-xs btn-info" data-val="'+row.id+'" title="修改"><i class="fa fa-edit"></i></button>';
        }
        return content;
    }

    function distribute(id) {
        layer.closeAll(); //关闭弹窗
        layer.load();
        $.post('/sea/physical/distribute/createDistribute.do',{id:id},function(data){
            if( data.ok ){
                layer.closeAll(); //关闭弹窗
                layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                    $planTable.bootstrapTable('refresh');

                });
            }else{
                layer.closeAll(); //关闭弹窗
                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){
                        layer.closeAll();
                    }});
            }
        });
    }

    function cancelAutoDistribute(id) {
        layer.closeAll(); //关闭弹窗
        layer.load();
        $.post('/sea/physical/distribute/auto/cancelAutoDistribute.do',{createId:id},function(data){
            if( data.ok ){
                layer.closeAll(); //关闭弹窗
                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                    $planTable.bootstrapTable('refresh');

                });
            }else{
                layer.closeAll(); //关闭弹窗
                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                        layer.closeAll();
                    }});
            }
        });
    }

    function autoDistribute(id) {
        layer.closeAll(); //关闭弹窗
        layer.load();
        $.post('/sea/physical/distribute/auto/AutoDistribute.do',{createId:id},function(data){
            if( data.ok ){
                layer.closeAll(); //关闭弹窗
                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                    $planTable.bootstrapTable('refresh');
                });
            }else{
                layer.closeAll(); //关闭弹窗
                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                        layer.closeAll();
                    }});
            }
        });
    }

    function sureAutoDistribute(id) {
        layer.closeAll(); //关闭弹窗
        layer.load();
        $.post('/sea/physical/distribute/auto/sureAutoDistribute.do',{createId:id},function(data){
            if( data.ok ){
                layer.closeAll(); //关闭弹窗
                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                    $planTable.bootstrapTable('refresh');
                });
            }else{
                layer.closeAll(); //关闭弹窗
                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                        layer.closeAll();
                    }});
            }
        });
    }
    function exportAuto(id) {
        window.location.href='/sea/physical/distribute/auto/export.do?ids='+id;
        layer.closeAll(); //关闭弹窗
    }
    function exportAuto2(id) {
        window.location.href='/sea/physical/distribute/auto/exportAuto.do?id='+id;
        layer.closeAll(); //关闭弹窗
    }
    function cancel(id) {
        layer.closeAll(); //关闭弹窗
        layer.load();
        $.post('/sea/physical/distribute/cancel.do',{createId:id},function(data){
            if( data.ok ){
                layer.closeAll(); //关闭弹窗
                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                    $planTable.bootstrapTable('refresh');

                });
            }else{
                layer.closeAll(); //关闭弹窗
                layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                        layer.closeAll();
                    }});
            }
        });
    }

    $('.search-btn').on('click',function(){
        $planTable.bootstrapTable('refresh', { url:'${ctxPath}/sea/physical/distribute/findByCnd.do?'+$('#search-form').serialize() });
    });

</script>
</body>
</html>
